package com.lemon.smartsafetynew.ui.fragment.hidden;

import android.os.Bundle;

import com.google.gson.Gson;
import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.ui.fragment.base.BasePullToRefreshFragment;
import com.lemon.smartsafetynew.entity.event.common.ParamsEvent;
import com.lemon.smartsafetynew.entity.event.common.ViewEvent;
import com.lemon.smartsafetynew.entity.hidden.SafeHiddenBo;
import com.lemon.smartsafetynew.entity.hidden.SafeHiddenDownloadVo;
import com.lemon.smartsafetynew.net.services.SafeHiddenServices;
import com.lemon.smartsafetynew.ui.adapter.hidden.SafeHiddenUploadHiddenListAdapter;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.lemon.smartsafetynew.util.user.AccountUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import kale.adapter.LemonRcvAdapter;

/**
 * @deprecated
 * Created by Andrew on 2017/9/8.
 */

public class SafeHiddenUploadHiddenFragment extends BasePullToRefreshFragment<SafeHiddenDownloadVo, List<SafeHiddenDownloadVo>>
        implements RecyclerTClickListener<SafeHiddenDownloadVo> {

    private List<SafeHiddenDownloadVo> mUploadList = new ArrayList<>();

    public static SafeHiddenUploadHiddenFragment newInstance() {

        Bundle args = new Bundle();

        SafeHiddenUploadHiddenFragment fragment = new SafeHiddenUploadHiddenFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onPullRefresh() {
        LemonRequestParams params = new LemonRequestParams("");
        params.put("status", "");
        params.put("up_to_official", "0");
        params.put("company_id", AccountUtil.getCompanyId());
        params.put("pageNum", currentPage);
        params.put("pageSize", pageSize);

        SafeHiddenServices
                .init()
                .getNotUploadHiddenList(RefreshType.REFRESH, params);
    }

    @Override
    public void onLoadMore() {
        LemonRequestParams params = new LemonRequestParams("");
        params.put("status", "");
        params.put("up_to_official", "0");
        params.put("company_id", AccountUtil.getCompanyId());
        params.put("pageNum", currentPage);
        params.put("pageSize", pageSize);

        SafeHiddenServices
                .init()
                .getNotUploadHiddenList(RefreshType.LOAD_MORE, params);
    }

    @Override
    public boolean goRefreshOnCreate() {
        return true;
    }

    @Override
    public LemonRcvAdapter<SafeHiddenDownloadVo> getLemonAdapter() {
        return new SafeHiddenUploadHiddenListAdapter(mDataList, this); //TODO
    }

    @Override
    public String getHttpTag() {
        return KeyConstants.URL_GET_SAFE_HIDDEN_NOT_UPLOAD_LIST;
    }

    @Override
    public void onItemClick(SafeHiddenDownloadVo data, int status) {
        if (status == SafeHiddenBo.Select.selected.getValue()) {
            mUploadList.add(data);
        } else {
            mUploadList.remove(data);
        }

        AppService.getBus().post(new ParamsEvent(KeyConstants.KEY_SAFE_HIDDEN_UPLOAD_NUM, String.valueOf(mUploadList.size())));

//        Log.e("yxy2", "===============================");
//        for(SafeHiddenDownloadVo safeHiddenDownloadVo : mUploadList) {
//            Log.e("yxy2", "safeHiddenDownloadVo ID = " + safeHiddenDownloadVo.getId());
//        }
//        Log.e("yxy2", "===============================");

    }

    /**
     * 获取上报隐患button事件
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onViewEvent(ViewEvent event) {
        if (KeyConstants.KEY_SAFE_HIDDEN_UPLOAD_HIDDEN.equals(event.getTag())) {
            uploadHidden();
        }
    }

    /**
     * 上报隐患
     */
    private void uploadHidden() {

        Gson gson = new Gson();
        List<String> list = new ArrayList<>();

        for (SafeHiddenDownloadVo data : mUploadList) {
            list.add(data.getId());
        }

        LemonRequestParams params = new LemonRequestParams("");

        params.put("danger_ids", gson.toJson(list));

        SafeHiddenServices
                .init()
                .reportSafeHidden(params);
    }



}
